package com.sky.controller.user;

import com.alibaba.fastjson.JSON;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单
 */
@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);

        Orders ordersDB=orderService.AAAAAAAApaySuccess(ordersPaymentDTO);//模拟下单成功

        Map map = new HashMap();
        map.put("type", 1);//消息类型，1表示来单提醒
        map.put("orderId", ordersDB.getId());
        map.put("content", "订单号：" + ordersDB.getNumber());

        //通过WebSocket实现来单提醒，向客户端浏览器推送消息
        webSocketServer.sendToAllClient(JSON.toJSONString(map));
        return Result.success();
    }

//    @PutMapping("/payment")
//    @ApiOperation("订单支付")
//    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
//        log.info("订单支付：{}", ordersPaymentDTO);
//        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
//        log.info("生成预支付交易单：{}", orderPaymentVO);
//        return Result.success(orderPaymentVO);
//    }


    //--T1--历史订单查询   /user/order/historyOrders
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(@RequestParam Map map){
        log.info("历史订单查询：{}", map);
        PageResult pageResult=orderService.getListOrderAndDetail(map);
        return Result.success(pageResult);
    }
    //--T2--查询订单详情   /user/order/orderDetail/{id}
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetailId(@PathVariable Long id){
        log.info("查询订单详情：{}", id);
        OrderVO orderVO=orderService.getOrderAndDetailById(id);
        return Result.success(orderVO);
    }
    //--T3--取消订单   /user/order/cancel/{id}
    @PutMapping("/cancel/{id}")
    public Result cancelOrder(@PathVariable Long id){
        log.info("取消订单：{}", id);
        orderService.cancelOrdersById(id);
        return Result.success();
    }

    //--T4--再来一单   /user/order/repetition/{id}
    @PostMapping("/repetition/{id}")
    public Result repetitionOrder(@PathVariable Long id){
        log.info("再来一单：{}", id);
        orderService.repetitionById(id);
        return Result.success();
    }


    //--T6--催单   /user/order/reminder/{id}
    @GetMapping("/reminder/{id}")
    public Result reminderOrder(@PathVariable Long id){
        log.info("催单：{}", id);
        orderService.reminderOrder(id);
        return Result.success();
    }

}
